﻿using System;
using System.Linq;
using TechnicalIndicators.Points;
using Utils;

namespace TechnicalIndicators.DateBased
{
	public class BarValueExtractor : GeneratorBase<Bar, DatedSeriePoint>
	{
		#region Attributes

		private BarToValueMode _extractionMode;

		#endregion

		#region Public Methods

		public BarValueExtractor() : base()
        {
        }

		public BarValueExtractor(ObservableCollection2<DatedSeriePoint> destination) : base(destination)
        {
        }

		public void Configure(BarToValueMode extractionMode)
		{
			_extractionMode = extractionMode;
		}

		protected override DatedSeriePoint CreatePoint(Bar sourceBar)
		{
			DatedSeriePoint result = null;

			switch (_extractionMode)
			{
				case BarToValueMode.None:
					throw new InvalidOperationException("You must set a valid working mode before using the extractor");

				case BarToValueMode.Open:
					result = new DatedSeriePoint(sourceBar.End, sourceBar.Open);
					break;

				case BarToValueMode.Close:
					result = new DatedSeriePoint(sourceBar.End, sourceBar.Close);
					break;

				case BarToValueMode.Max:
					result = new DatedSeriePoint(sourceBar.End, sourceBar.Maximum);
					break;

				case BarToValueMode.Min:
					result = new DatedSeriePoint(sourceBar.End, sourceBar.Minimum);
					break;

				case BarToValueMode.Average:
					result = new DatedSeriePoint(sourceBar.End, sourceBar.Values.Sum() / sourceBar.Values.Count);
					break;

			}

			return result;
		}

		#endregion

	}
}
